<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%
	String path = request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>微博线索</title>
<link rel="stylesheet" type="text/css" href="<%=path%>/hplus/css/bootstrap.min.css"/>
<link rel="stylesheet" type="text/css" href="<%=path%>/hplus/css/style.min.css"/>
<link rel="stylesheet" type="text/css" href="<%=path%>/hplus/css/font-awesome.min93e3.css"/>
<link rel="stylesheet" type="text/css" href="<%=path%>/bc/css/clue.css"/>
</head>
<body class="gray-bg">
<div class="container-fluid">
	<div class="row weibo1">
		<div class="col-sm-12">
			<div class="ibox float-e-margins">
	            <div class="ibox-title">
	                <h5><span id="weiboCount">0</span> 条线索源于微博</h5>
	            </div>
	            <div class="ibox-content">
	            	<div class="row">
	            		<div class="col-sm-5">
	            			<ul class="progressbarUl2"></ul>
	            		</div>
	            	</div>
	            	<div id="nothing1" style="text-align: center;display: none;">
           				<img src="<%=path%>/images/icon_wb.png">
           				<p>您还没有绑定新浪微博 <a href="<%=path%>/ui/wbbangding">去绑定&gt;</a></p>
           			</div>
           			
           			<div id="nothing2" style="text-align: center;display: none;">
           				<img src="<%=path%>/images/icon_wb.png">
           				<p>您还没有添加关键字 <a href="<%=path%>/ui/wbbangding">去添加&gt;</a></p>
           			</div>
	            </div>
	        </div>
		</div>
	</div>
	
	<div class="row weibo2">
	    <div class="col-sm-12">
	        <div class="ibox float-e-margins">
	            <div class="ibox-title">
	               <ul class="ulH">
						<li>暂无活动</li>
					</ul>
	            </div>
	            <div class="ibox-content">
	                <table class="table table-hover">
	                	<thead>
	                        <tr>
	                            <th>姓名</th>
	                            <th>行为</th>
	                            <th>来源</th>
	                            <th>时间</th>
	                            <th>操作</th>
	                        </tr>
	                    </thead>
	                    <tbody>
	                    </tbody>
	                </table>
	            </div>
	        </div>
	    </div>
	</div>
</div>
</body>
<script type="text/javascript" src="<%=path%>/bc/bootstarp/js/jquery.min.js"></script>
<script type="text/javascript" src="<%=path%>/bc/js/clue_title.js"></script>
<script type="text/javascript" src="<%=path%>/js/page.js"></script>
<script type="text/javascript" src="<%=path%>/js/jquery.highlight.js"></script>
<script type="text/javascript">
var path = "<%=path%>";
var startIndex = 0;
var pageSize = 10;
var keyWord = "";
var currentPage = 1;
var pageCount = 0;

var customerId = 0;	//客户id
var clueWeiboId = 0;//微博线索id

$(function(){
	init();
    
	$(document).on('click','.s_all_title li',function(){
		if(keyWord != $(this).attr("keyWord")){
	        keyWord = $(this).attr("keyWord");
	        init();
		}
    });
	
	/*聊天点击开始*/
	$(document).on("click",".s_icon_dialogue",function(){
		//初始化数据，全部设置0
		top.customerId = 0;
		top.actionId = 0;
		top.clueWeiboId = 0;
		top.writeOffId = 0;
		
		customerId = $(this).attr("customerId");
		clueWeiboId = $(this).attr("clueWeiboId");
		
		top.customerId = customerId;
		top.clueWeiboId = clueWeiboId;
		
		var d = top.dialog({
			fixed: true,
			zIndex: 10000,
			padding:0,
			width:1172,
			height:568,
			title:'沟通记录',
			url:path+"/api/customer_action/customerChatView",
			cancelDisplay: false,
			cancel: function () {
				if(top.customerChatChange == 1){
					top.customerChatChange = 0;
				}
				top.customerId = 0;
				top.actionId = 0;
				top.clueWeiboId = 0;
				return true;
			}
		});
		d.showModal();
	});
	/*聊天点击结束*/
});

function init(){
	$.post("${pageContext.request.contextPath}/api/weibo/showfirstpage",function(data){
		data = eval(data);
		if(data.showstatus == 1){
			;
		}else if(data.showstatus == 0){
			$(".m_xmanage>a:eq(1)").css("display","none");
		}
	});
	
	$.post(path+'/api/clue_customer/clue_weibo',{"startIndex":startIndex,"pageSize":pageSize,"keyWord":keyWord},function(data){
		data = JSON.parse(data);
		
		setFirst(data.key_word_list,data.weibo_keyword_count);
		
		setSecond(data.weibo_clue_list,data.weibo_keyword_count);
		
		page(data.totalCount);
	});
}

function setFirst(key_word_list,keyword_count){
	//首先判断是否绑定微博
	$.ajax({
		async:false,
		url:"<%=path%>/api/weibo/changebind",
		type:"POST",
		dataType:"json",
		success:function(data){
			if(data.error_code==1 || data.RemainingDay<=0){
				$("#weiboCount").text(total);
				$(".weibo1 #nothing1").show();
			}
		}
	});
	
	if(!$(".weibo1 #nothing1").is(":hidden")){
		$(".weibo2").hide();
		return ;
	}
	
	
	
	var total = 0;
	
	var startStr = "";
	var endStr = "";
	
	$(".weibo2 ul.ulH").html('<li keyWord=""><span>全部</span></li>');
	for ( var i = 0; i < keyword_count.length; i++) {
		var a = keyword_count[i];
		total += a.count;
	}
	
	for ( var i = 0; i < keyword_count.length; i++) {
		var a = keyword_count[i];
		for ( var j = 0; j < key_word_list.length; j++ ) {
			var b = key_word_list[j];
			
			if(a.keyWord == b){
				$(".weibo2 ul.ulH").append('<li keyWord="'+a.activityName+'"><span>'+a.activityName+'</span></li>');
				key_word_list.splice(j,1);
				startStr += '<li keyWord="'+a.keyWord+'"><span>'+a.keyWord+'</span><div><div style="width:'+length_baifenbi+'%;"></div></div><span>'+a.count+'条</span></li>';
			}
		}
	}
	for ( var i = 0; i < key_word_list.length; i++) {
		var a = key_word_list[i];
		if(a != ""){
			endStr += '<li keyWord="'+a.keyWord+'"><span>'+a.keyWord+'</span><div><div style="width:'+length_baifenbi+'%;"></div></div><span>'+a.count+'条</span></li>';
			$(".weibo2 ul.ulH").append('<li keyWord="'+a.activityName+'"><span>'+a.activityName+'</span></li>');
		}
	}

	$("#weiboCount").text(total);
	
	if(startStr+endStr != ''){
		$(".activity1 .progressbarUl2").html(startStr+endStr);
	}else{
		$(".weibo1 #nothing2").show();
		$(".weibo2").hide();
	}
}

function setSecond(weibo_clue_list,keyword_count){
	var tr = "";
	for(var i = 0; i < weibo_clue_list.length; i++){
		var a = weibo_clue_list[i];
		tr += '<tr><td><img src="'+a.avatar+'"/><a href="javascript:;">'+a.screenName+'</a></td><td>'+a.content+'</td><td width="70px" class="k_time">';
		var now = new Date();
		var old = new Date(a.createTimeStr);
		var m_date = parseInt((now.getTime() - old.getTime())/1000/60);
		if(m_date >= 100){
			m_date = a.createTimeStr;
		}else{
			m_date += "分钟前";
		}
		tr += m_date;
		tr +='</td><td width="70px">'
			+'<shiro:hasPermission name="customer_clue_delete_button"><a href="javascript:;" onclick="delClueWeibo('+a.id+')" class="s_icon_delect"></a></shiro:hasPermission>'
			+'<shiro:hasPermission name="customer_clue_chat_button"><a customerId="'+a.customerId+'" actionId="0" clueWeiboId="'+a.id+'" href="javascript:;" class="s_icon_dialogue"></a></shiro:hasPermission></td></tr>;'
	}
	
	$(".weibo2 tbody").html(tr);
	
	for ( var i = 0; i < keyword_count.length; i++) {
		var a = keyword_count[i];
		$('.weibo2 tbody').highlight(a.keyWord);
	}
}

function delClueWeibo(id){
	$(".j_popup_box").show();
	$(".j_popup_box .j_popup_box_confirm").unbind("click");
	$(".j_popup_box .j_popup_box_confirm").one("click",function(){
		$.post("<%=path%>/api/clue_customer/delClueWeibo",{clueWeiboId:id},function(data){
			init();
			$(".j_popup_box").hide();
		});
	});
}
</script>
</html>